package cn.edu.neusoft.yuejian12.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import cn.edu.neusoft.yuejian12.R;
import cn.edu.neusoft.yuejian12.Activity.TrainInfoActivity;
import cn.edu.neusoft.yuejian12.JavaBean.TrainInfo;

import java.util.List;


/**
 * Created by owner on 2017/10/10.
 */

public class DegreeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;

    private List mDataList;//数据存储
    private LayoutInflater mLayoutInflater;//准备布局

    public DegreeAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_trainlist, parent, false);
        return new DegreeAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final TrainInfo entity = (TrainInfo) mDataList.get(position);
        if (null == entity)
            return;
        DegreeAdapter.ViewHolder viewHolder = (DegreeAdapter.ViewHolder) holder;
        viewHolder.train_name.setText(entity.getName());
        String address="http://172.24.10.175/workout/Uploads/"+entity.getThumb();
        if(!entity.getThumb().equals("")){
            Picasso.with(mContext).load(address).into(viewHolder.train_thumb);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,TrainInfoActivity.class);
                intent.putExtra("train_id",entity.getId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView train_thumb;
        TextView train_name;
        LinearLayout train_container;

        public ViewHolder(View itemView) {
            super(itemView);
            train_container = (LinearLayout) itemView.findViewById(R.id.train_container);
            train_name = (TextView) itemView.findViewById(R.id.train_name);
            train_thumb = (ImageView) itemView.findViewById(R.id.train_thumb);

        }
    }
}
